<template>
    <div>
        <div>
            <el-input placeholder="请输入角色英文名..." v-model="role.name" size="mini" style="width: 300px; margin-right: 8px" >
            <template slot="prepend">ROLE_</template>
        </el-input>
            <el-input placeholder="请输入英文名..." v-model="role.nameZh" size="mini" style="width: 300px;margin-right: 8px"></el-input>
            <el-button type="primary" size="mini" icon="el-icon-plus" @click="addRole">添加角色</el-button>
        </div>
        <div class="collapse">
            <el-collapse v-model="activeName" accordion @change="change">
                <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r,index) in roles" :key="index">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问的资源</span>
                            <el-button style="float: right; padding: 3px 0;color: red" icon="el-icon-delete" type="text"
                            @click="deleteRole(r)"></el-button>
                        </div>

                        <div>
                                <el-tree
                                        :key="index"
                                        ref="tree"
                                        :data="allmenus"
                                        :props="defaultProps"
                                        show-checkbox
                                        node-key="id"
                                        :default-checked-keys="selectMenus"
                                ></el-tree>
                            <div style="display: flex;justify-content: flex-end">
                                <el-button size="mini" v-model="activeName" @click="cancelUpdate">取消修改</el-button>
                                <el-button type="primary" size="mini" @click="doUpdate(r.id,index)">确认修改</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-collapse-item>

            </el-collapse>
        </div>
    </div>

</template>

<script>
    export default {
        name: "PerMissMana",
        data(){
            return{
                role:{
                    name:'',
                    nameZh:''
                },
                roles:[],
                allmenus:[],
                selectMenus:[],
                activeName:-1,
                defaultProps:{
                    children:'children',
                    label:'name'
                }
            }
        },
        mounted() {
          this.initRoles();
        },

        methods:{
            deleteRole(role){
                this.$confirm('是否删除【'+role.nameZh+'】角色？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/permission/role/"+role.id).then(resp=>{
                        if (resp){
                            this.initRoles();
                        }

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });            },
            addRole(){
                if (this.role.name&&this.role.nameZh){
                    this.postRequest("/system/basic/permission/role/",this.role).then(resp=>{
                        if (resp){
                            this.initRoles();
                            this.role.name='';
                            this.role.nameZh='';

                        }
                    })
                }
            },
            cancelUpdate(){
              this.activeName=-1;
            },
            doUpdate(rid,index){
                let tree = this.$refs.tree[index];
                let selectedKeys = tree.getCheckedKeys(true);
                let url = '/system/basic/permission/?rid='+rid;
                selectedKeys.forEach(key=>{
                    url+='&mids='+key;
                })
                this.putRequest(url).then(resp=>{
                    if (resp){
                        this.activeName=-1;
                    }
                })
            },
            change(rid){
                if (rid){
                    this.initMenus();
                    this.initSelectMenus(rid);
                }
            },
            initSelectMenus(rid){
                this.getRequest("/system/basic/permission/mids/"+rid).then(resp=>{
                    if (resp){
                        this.selectMenus=resp;
                    }
                })
            },

            initRoles(){
                this.getRequest("/system/basic/permission/").then(resp=>{
                    if (resp){
                        this.roles=resp;
                    }
                })
            },
            initMenus(){
                this.getRequest("/system/basic/permission/menu").then(resp=>{
                    if (resp){
                        this.allmenus=resp;
                    }
                })
            }
        }
    }
</script>

<style>
    .collapse{
        margin-top: 10px;
        width: 700px;
    }

</style>